import json
from django.http.response import JsonResponse
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response

from apps.case.core import case_insert, case_query, case_delete, case_update, query_policy_owner, \
    query_case_details_by_policy_owner, query_not_case_info, query_case_name



@api_view(('POST',))
@permission_classes((IsAuthenticated,))
def case_manage(request):
    if request.method == 'POST':
        request.params = json.loads(request.body)
        action = request.params['action']
        if action == 'insert':
            return case_insert(request)
        if action == 'query':
            return case_query(request)
        if action == 'delete':
            return case_delete(request)
        if action == 'update':
            return case_update(request)
        if action == 'query_case_name':
            return query_case_name()
        else:
            return JsonResponse({'ret': 1, "err": "", 'msg': "action参数为必传值"}, safe=False)


@api_view(('POST',))
@permission_classes((IsAuthenticated,))
def policy_owner(request):
    # action = request.POST.get("action", None)
    request.params = json.loads(request.body)
    action = request.params['action']
    print(action)
    if action == 'query':
        return query_policy_owner()
    if action == 1:
        return query_case_details_by_policy_owner(request.params)
    else:
        return JsonResponse({'ret': 1, "err": "", 'msg': "action参数为必传值"}, safe=False)

@api_view(('POST',))
@permission_classes((IsAuthenticated,))
def query_not_case(request):
    # print(request.body.decode('utf8'))
    request.params = json.loads(request.body)
    action = request.params['action']
    # action = request.POST.get("action", None)
    print(action)
    if action == 'query':
        return query_not_case_info(request.params)
    else:
        return JsonResponse({'ret': 1, "err": "", 'msg': "action参数为必传值"}, safe=False)